<?php
/**
 * Created by PhpStorm.
 * User: mars
 * Date: 2019/1/20
 * Time: 4:30 PM
 */

namespace api\models;

use Yii;

class UserCertification extends \common\models\UserCertification
{

    public function fields()
    {
        /**@var UserCertification $model  */
        return [
            'id',
//            'uid',
//            'type',
            'real_name',
            'phone',
            'id_card',
            'img'=>function($model){
                $cdn = Yii::$app->get('cdn');
                $cdn->host = Yii::$app->feehi->website_url;
                return $cdn->getCdnUrl($model->img0->image);
            },
            'status',
            'create_at'=>function($model){
                return date('Y-m-d H:i:s',$model->created_at);
            },
            'notify',
        ];
    }


    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['type'], 'required'],
            [['uid', 'type', 'status', 'img', 'created_at', 'updated_at'], 'integer'],
            [['real_name', 'id_card', 'phone', 'token', 'notify'], 'string', 'max' => 255],
            [['img'], 'exist', 'skipOnError' => true, 'targetClass' => Image::className(), 'targetAttribute' => ['img' => 'id']],
//            [['uid'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['uid' => 'id']],
        ];
    }


    public function beforeSave($insert)
    {
        if ($insert){
            $this->uid = \Yii::$app->user->id;
            $this->status = self::WAIT_STATUS;
            $this->created_at = time();
        }else{
            $this->updated_at = time();
        }

        return parent::beforeSave($insert);
    }


    public function afterSave($insert, $changedAttributes)
    {

        parent::afterSave($insert, $changedAttributes);
    }

//    public function

}